import { defineStore } from 'pinia'
import axios from 'axios'

export const useProductStore = defineStore('product', {
  state: () => ({
    products: [],
    currentProduct: null,
    categories: []
  }),

  actions: {
    async fetchProducts(category = null, minPrice = null, maxPrice = null) {
      try {
        const params = {}
        if (category) params.category = category
        if (minPrice) params.minPrice = minPrice
        if (maxPrice) params.maxPrice = maxPrice

        const response = await axios.get('http://localhost:3000/api/products', { params })
        this.products = response.data
      } catch (error) {
        console.error('获取商品列表失败:', error)
      }
    },

    async fetchProductById(id) {
      try {
        const response = await axios.get(`http://localhost:3000/api/products/${id}`)
        this.currentProduct = response.data
      } catch (error) {
        console.error('获取商品详情失败:', error)
      }
    },

    async fetchCategories() {
      try {
        const response = await axios.get('http://localhost:3000/api/products/categories')
        this.categories = response.data
      } catch (error) {
        console.error('获取商品分类失败:', error)
      }
    }
  }
}) 